package pos.data;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.DatabaseMetaData;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.AbstractCollection;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Properties;
import java.io.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import pos.domain.*;
import javax.swing.JOptionPane;
import javax.swing.text.html.HTMLDocument.Iterator;

import sun.font.Script;

import com.mysql.jdbc.Statement;

//import com.sun.xml.internal.bind.v2.schemagen.xmlschema.List;

import pos.domain.*;

public class FilePmDAO implements IPmDAO {

	private static Properties dbprops = new Properties();

	public FilePmDAO() {
		InputStream is = null;
		try {
			is = Thread.currentThread().getContextClassLoader()
					.getResourceAsStream("dbconfiguration.properties");
			dbprops.load(is);
			// dbprops.load(new
			// FileInputStream("/src/dbconfiguration.properties"));
		} catch (Exception e1) {
			System.err
					.println("El fichero de propiedades de la BDs no se ha encontrado");
			e1.printStackTrace();
		}

	}

	public void cambia(String a, String b, String c, String d) {
		try {

			String path = (String) Thread.currentThread()
					.getContextClassLoader().getResource(
							"dbconfiguration.properties").getPath();

			String[] palabra = path.split("\\/");
			String pathAux = "";
			int j = 0;
			while (j <= palabra.length - 2) {
				pathAux = pathAux + palabra[j] + "/";
				j++;
			}

			java.util.Properties dbprops = new java.util.Properties();
			dbprops.setProperty("dBUri", a);
			dbprops.setProperty("driverName", b);
			dbprops.setProperty("password", c);
			dbprops.setProperty("username", d);

			FileOutputStream fos = new FileOutputStream(path);
			dbprops.store(fos, path);

			fos.close();

		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public String getDburi() {
		String uri = "";
		InputStream is = null;
		try {
			is = Thread.currentThread().getContextClassLoader()
					.getResourceAsStream("dbconfiguration.properties");
			dbprops.load(is);
			//dbprops
				//	.load(new FileInputStream("/src/dbconfiguration.properties"));
		} catch (Exception e1) {
			System.err
					.println("El fichero de propiedades de la BDs no se ha encontrado");
			e1.printStackTrace();
		}

		try {
			uri = dbprops.getProperty("dBUri");

		} catch (Exception e) {
			System.err.println("Unable to register JDBC Driver");
			e.printStackTrace();
		}
		return uri;

	}

	public String getDrivername() {
		String uri = "hola";
		InputStream is = null;
		try {
			is = Thread.currentThread().getContextClassLoader()
					.getResourceAsStream("dbconfiguration.properties");
			dbprops.load(is);
			//dbprops
				//	.load(new FileInputStream("/src/dbconfiguration.properties"));
		} catch (Exception e1) {
			System.err
					.println("El fichero de propiedades de la BDs no se ha encontrado");
			e1.printStackTrace();
		}

		try {
			uri = dbprops.getProperty("driverName");
			// is.close();

		} catch (Exception e) {
			System.err.println("Unable to register JDBC Driver");
			e.printStackTrace();
		}
		return uri;

	}

	public String getPassword() {
		String uri = "hola";
		InputStream is = null;
		try {
			is = Thread.currentThread().getContextClassLoader()
					.getResourceAsStream("dbconfiguration.properties");
			dbprops.load(is);
			//dbprops
				//	.load(new FileInputStream("/src/dbconfiguration.properties"));
		} catch (Exception e1) {
			System.err
					.println("El fichero de propiedades de la BDs no se ha encontrado");
			e1.printStackTrace();
		}

		try {
			uri = dbprops.getProperty("password");
			// is.close();

		} catch (Exception e) {
			System.err.println("Unable to register JDBC Driver");
			e.printStackTrace();
		}
		return uri;

	}

	public String getUsername() {
		String uri = "";
		InputStream is = null;
		try {
			is = Thread.currentThread().getContextClassLoader()
					.getResourceAsStream("dbconfiguration.properties");
			dbprops.load(is);
			//dbprops
				//	.load(new FileInputStream("/src/dbconfiguration.properties"));
		} catch (Exception e1) {
			System.err
					.println("El fichero de propiedades de la BDs no se ha encontrado");
			e1.printStackTrace();
		}

		try {
			uri = dbprops.getProperty("username");
			// is.close();

		} catch (Exception e) {
			System.err.println("Unable to register JDBC Driver");
			e.printStackTrace();
		}
		return uri;

	}
	
	public String getSchemaName(String dburi){
		String res = null;
		
		//TODO	
		return res;
	}

}